<?
  include_once "config.php";
  include_once "dbconn.php";

  switch($_GET['ajax_method']){

    case 'INSERT_QUESTION':
        $query = "INSERT INTO question(Uid, QText, Latitude, Longitude, CreatedAt)";
        $query .= " VALUES(1, '".urldecode($_GET['msg'])."', ".$_GET['latitude'].", ".$_GET['longitude'].", '".date('Y-m-d H:i:s')."')";

        $result = mysql_query($query);
        if ($result) {
            echo json_encode(array('true' => "OK"));
        } else {
            echo json_encode(array('false' => $query));
        }
        break;

    case 'INSERT_ANSWER':
        $query = "INSERT INTO answer(Qid, AText, CreatedAt)";
        $query .= " VALUES(".$_GET['qid'].", '".urldecode($_GET['msg'])."', '".date('Y-m-d H:i:s')."')";

        $result = mysql_query($query);
        if ($result) {
            echo json_encode(array('true' => "OK"));
        } else {
            echo json_encode(array('false' => $query));
        }
        break;

    case 'GET_QUESTION':
        $query = "SELECT * FROM question ORDER BY Qid DESC";
        $result = mysql_query($query);
        $count = mysql_num_rows($result);
        $index = 1;

        echo "[";
        while($row = mysql_fetch_array($result)){
            echo '{"qid":'.$row['Qid'].', "uid":'.$row['Uid'].', "msg":"'.$row['QText'].'"'.',"latitude":'.$row['Latitude'].',"longitude":'.$row['Longitude'].',"created_at":"'.$row['CreatedAt'].'"}';
            if($index != $count){ 
		    echo ",";
            }
            $index++; 
        }
        echo "]";
        break;

    case 'GET_ANSWER':
        $query = "SELECT * FROM answer WHERE Qid=".$_GET['qid']." ORDER BY Aid desc";
        $result = mysql_query($query);
        $count = mysql_num_rows($result);
        $index = 1;

        echo "[";
        while($row = mysql_fetch_array($result)){
            echo '{"qid":'.$row['Qid'].', "aid":'.$row['Aid'].', "msg":"'.$row['AText'].'","created_at":"'.$row['CreatedAt'].'"}';
            if($index != $count){ 
		    echo ",";
            }
            $index++; 
        }
        echo "]";
        break;
    case 'SEND_YOZM':
	$oauth->setToken($_SESSION['access_token'],$_SESSION['access_token_secret']);
	//$result = $oauth->fetch($api_url."/yozm/v1_0/message/add.json?message=".urldecode($_GET['msg']));
	$result = $oauth->fetch($api_url."/yozm/v1_0/message/add.json?message=".$_GET['msg']);
        echo $result;
        break;
  }
?>
